<?php

    // any methods you put here will be globally available to your controllers and templates
function has_access_to($acl_name)
{      
    if ( empty($_SESSION['user_id']) )
    {
        return false;
    }
    use_model('acl','users');  
    $acl   = new Acl();
    $users = new Users();
    $pass  = $users->get_user_password_by_id( (int) $_SESSION['user_id'] );
    $hashed_acl = create_hash($acl_name.$pass);
    if ( isset($_SESSION[$hashed_acl]) && $_SESSION[$hashed_acl] )
    {
        return true;
    } else {        
        $has_access = $acl->has_access_to($acl_name,$_SESSION['user_id']);
        $_SESSION[$hashed_acl] = $has_access;
        return $has_access;
    }
        
}
function has_role($role)
{
    if ( empty($_SESSION['user_id']) )
    {
        return false;
    }
    use_model('roles');
    $roles = new Roles();
    return $roles->has_role($role,$_SESSION['user_id']);
}
function get_meta_keywords($w=null)
{
    if(!is_null($w)) return htmlentities($w);
}

function get_meta_description($w=null)
{
    if(!is_null($w)) return htmlentities($w);
}

function get_page_title($w=null)
{
    if(!is_null($w)) {
        return htmlentities($w);
    } else {
        return 'Welcome to pixel app';
    }
}

function busy_image()
{
    return "<img src='".get_installed_path()."/images/admin/spinner_big.gif' id='busy' style='display:none'>";
}

// this function is best used when you want to display news
// in another location (other then the news controller)
/*
$news = get_news();
if(!empty($news)) {
    foreach($news as $n) {
               printf('<li><b>%s</b><br>%s</li>',
                date_format($n['publish_date']),
                $n['title']);
    }
}
*/
function get_news()
{
    use_model('news');
    $news = new News();
    return $news->find(array(
                       'fields'=>array('index_name','title','publish_date','content'),
                       'limit'=>3,
                       'order'=>'publish_date DESC'));
}
?>